$(document).ready(function () {
    initFunction();
})

function deleteFunction(id) {

    if(window.confirm('你确定删除吗？')){

        $.ajax({
            url:"/api/deleteFunction/"+id,
            type:"Delete",
            dataType:"json",
            success:function(data){
                initFunction();
                layer.msg(data.message,{icon: 5});
            },
            error:function () {
                layer.msg("系统错误！",{icon: 5});
            }
        });
    }

}

function initFunction() {
    $.ajax({
        type: 'get',
        url: '/api/function',
        dataType:"json",
        data:{
          "pageSize":20,
          "pageNumber":0
        },
        success: function (result) {
            var data = result.data;
            $("#function_list").html("")
            var val = "";
            data.forEach(function(value,index){

                val = val+ "<tr><td><a href=\"/context/"+value.fid+"\" class=\"btn_a\">"+value.name+"</a>" +
                    "</td><td>"+value.url+"</td><td>"+value.pid+"</td><td>"+value.description+"</td><td>" +
                    "<a role=\"button\" href=\"/api/queryfunction/"+value.fid+"\" class=\"btn_edit\"  style=\"border: 1px solid #F37B1D;padding: 2px 4px;margin: 0px 4px;\">" +
                    "<span class='glyphicon glyphicon-file'></span>编辑</a>" +
                    "<a role=\"button\" class=\"btn_delete\" onclick='deleteFunction("+value.fid+")' style=\"border: 1px solid #e7505a;padding: 2px 4px;margin: 0px 4px;\">" +
                    "<span class='glyphicon glyphicon-trash'></span>删除</a></td></tr>";
            });
            $("#function_list").append(val)
        },
        error:function () {
            layer.msg("系统错误！",{icon: 5});
        }
    });

    $("#submit").click(function () {
        $.ajax({
            type: 'POST',
            url: '/api/addfunction',
            dataType:"json",
            data: {
                'name': $("#name").val(),
                'url': $("#url").val(),
                'description':$("#description").val(),
                'zindex':$("#zindex").val()
            },
            success: function (result) {
                if (result.code==200){
                    layer.msg(result.message,{icon: 1});
                    window.setTimeout(function () {
                        window.location.href="/admin/function"
                    },1000)
                } else {
                    layer.msg(result.message,{icon: 5});
                }

            },
            error:function () {
                layer.msg("系统错误！",{icon: 5});
            }
        });
    });

    $("#update").click(function () {
        $.ajax({
            type: 'POST',
            url: '/api/updatefunction',
            dataType:"json",
            data: {
                'id':$("#id").val(),
                'name': $("#name").val(),
                'url': $("#url").val(),
                'description':$("#description").val(),
                'zindex':$("#zindex").val()
            },
            success: function (result) {
                if (result.code==200){
                    layer.msg(result.message,{icon: 1});
                    window.setTimeout(function () {
                        window.location.href="/admin/function"
                    },1000)
                } else {
                    layer.msg(result.message,{icon: 5});
                }

            },
            error:function () {
                layer.msg("系统错误！",{icon: 5});
            }
        });
    });


}

